'use strict';

angular.module('app')
    .controller('postTaskDetailController', ['$scope', '$rootScope', '$state', '$timeout',
        function($scope, $rootScope, $state, $timeout) {
            var title = "任务审核详情";
            var id = $state.params.id;
            $scope.title = $rootScope.title = title;
            $scope.loading = true;
            activate(id);
            // 初始化页面
            function activate(id) {
                validate();
                $scope.loading = true;
                $.ajax({
                    url : $scope.app.project_name + '/postTask/read/detail',
                    data: {'taskId': id}
                }).then(function(result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.record = result.data;
                        console.log("=====");
                        console.log(result.data);
                        var status=result.data.status;
                        var isApproval=result.data.isApproval;
                        if((status=='1' || status=='2') && isApproval=='Y'){
                            $scope.btn=true;
                        }
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
            }
            $scope.approveService = function(){
                $scope.firstApply1 = true;
                $scope.taskRemark="";
            }



            function validate() {
                jQuery("#btnform").validate({
                    rules: {
                        taskRemark: {
                            required: true,
                            maxlength:300
                        }
                    },
                    messages: {
                        taskRemark: {
                            required: '拒绝理由不能为空',
                            maxlength: '拒绝理由长度不可超过300'
                        }
                    },
                    submitHandler: function () {
                        $scope.submit();
                    }
                });
            }
            $scope.submit=function(){
                var befstatus=$scope.record.status;
                var status='';
                //当前状态为1  时  不通过则状态为3
                if(befstatus=='1'){
                    status='5';
                }else if(befstatus=='2'){
                    status='5';
                }
                var remark=$scope.taskRemark;
                //postUserId
                var postUserId=$scope.record.postUserId;
                var param={"taskId":id,"postId":$scope.record.postId,"befstatus":befstatus,"status":status,"remark":remark,"postUserId":postUserId};
                var m = param;
                console.log(param);
                if(m){
                    $scope.isDisabled = true;//提交disabled
                    $.ajax({
                        url : $scope.app.project_name + '/postTask/read/update' ,
                        data: param
                    }).then(callback);
                }

                function callback(result){
                    console.log(result);

                    if(result.httpCode ==200){//成功

                        $timeout(function(){
                            $state.go('main.forum.task.list');
                        },200);
                    }else{
                        $scope.isDisabled = false;
                        alert("请求失败");
                    }
                    $scope.$apply();
                }
            }


            $scope.submitSuccess=function(){
                var befstatus=$scope.record.status;
                var status='';
                //当前状态为1  时  不通过则状态为3
                if(befstatus=='1'){
                    status='4';
                }else if(befstatus=='2'){
                    status='4';
                }
                var remark=$scope.taskRemark;
                var param={"taskId":id,"postId":$scope.record.postId,"befstatus":befstatus,"status":status,"remark":remark};
                var m = param;
                console.log(param);
                if(m){
                 $scope.isDisabled = true;//提交disabled
                 $.ajax({
                 url : $scope.app.project_name + '/postTask/read/update' ,
                 data: param
                 }).then(callback);
                 }

                function callback(result){
                    console.log(result);

                    if(result.httpCode ==200){//成功

                        $timeout(function(){
                            $state.go('main.forum.task.list');
                        },200);
                    }else{
                        $scope.isDisabled = false;
                        alert("请求失败");
                    }
                    $scope.$apply();
                }
            }


            var img_index = 0;
            var img_src = "";

            $scope.img=function() {

                //关闭
                $(".photo-close").click(function() {
                    $(".photo-mask").hide();
                    $(".photo-panel").hide();
                });
                //下一张
                $(".photo-panel .photo-div .arrow-next").click(function() {
                    img_index++;
                    if(img_index >= $(".demo li img").length) {
                        img_index = 0;
                    }
                    img_src = $(".demo li img").eq(img_index).attr("src");
                    photoView($(".demo li img"));
                });
                //上一张
                $(".photo-panel .photo-div .arrow-prv").click(function() {
                    img_index--;
                    if(img_index < 0) {
                        img_index = $(".demo li img").length - 1;
                    }
                    img_src = $(".demo li img").eq(img_index).attr("src");
                    photoView($(".demo li img"));
                });

            }

            //如何调用？
            $scope.imglook=function(event) {
                $(".photo-mask").show();
                $(".photo-panel").show();
                img_src = $(event.target).attr("src");
                img_index = $(event.target).index();
                photoView($(event.target));
            }
            //自适应预览
            function photoView(obj) {
                if($(obj).width() >= $(obj).height()) {
                    $(".photo-panel .photo-div .photo-img .photo-view-h").attr("class", "photo-view-w");
                    $(".photo-panel .photo-div .photo-img .photo-view-w img").attr("src", img_src);
                } else {
                    $(".photo-panel .photo-div .photo-img .photo-view-w").attr("class", "photo-view-h");
                    $(".photo-panel .photo-div .photo-img .photo-view-h img").attr("src", img_src);
                }
                //此处写调试日志
                console.log(img_index);
            }
            $scope.img();
    } ]);